package com.fs.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.fs.annotation.SysLog;
import com.fs.pojo.SysAdminMenus;
import com.fs.service.SysAdminMenuService;
import com.fs.vo.SysResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(tags = "菜单管理")
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private SysAdminMenuService sysAdminMenuService;

    @SysLog("查看菜单列表")
    @ApiOperation(value = "查看菜单列表")
    @RequiresPermissions({"admin:menu:list"})
    @GetMapping("/list")
    public SysResult menuList(){
        return sysAdminMenuService.getAll();
    }

    @SysLog("选择某个菜单")
    @ApiOperation(value = "选择某个菜单")
    @RequiresPermissions({"admin:menu:list"})
    @GetMapping("/select")
    public SysResult menuSelect(){
        return sysAdminMenuService.menuSelect();
    }


    /**
     * 菜单信息
     */
    @SysLog("菜单信息")
    @ApiOperation(value = "菜单信息")
    @GetMapping("/info/{menuId}")
    @RequiresPermissions("admin:menu:info")
    public SysResult info(@PathVariable("menuId") Integer menuId){
        SysAdminMenus menu = sysAdminMenuService.getById(menuId);
        return SysResult.success(menu);
    }

    /**
     * 保存
     */
    @SysLog("菜单保存")
    @ApiOperation(value = "菜单保存")
    @RequiresPermissions({"admin:menu:save"})
    @PostMapping("/save")
    public SysResult save(@RequestBody SysAdminMenus menu){
        //数据校验
        int verifyForm = verifyForm(menu);
        if(verifyForm==1){
            return SysResult.fail("菜单名称不能为空");
        }else if(verifyForm==2){
            return SysResult.fail("上级菜单不能为空");
        }else  if (verifyForm==3){
            return SysResult.fail("菜单URL不能为空");
        }else if(verifyForm==4){
            return SysResult.fail("上级菜单只能为目录类型");
        }else if(verifyForm==5){
            return SysResult.fail("上级菜单只能为菜单类型");
        }
        sysAdminMenuService.save(menu);

        return SysResult.success();
    }

    /**
     * 修改
     */
    @SysLog("菜单修改")
    @ApiOperation(value = "菜单修改")
    @PostMapping("/update")
    @RequiresPermissions("admin:menu:update")
    public SysResult update(@RequestBody SysAdminMenus menu){
        //数据校验
        int verifyForm = verifyForm(menu);
        if(verifyForm==1){
            return SysResult.fail("菜单名称不能为空");
        }else if(verifyForm==2){
            return SysResult.fail("上级菜单不能为空");
        }else  if (verifyForm==3){
            return SysResult.fail("菜单URL不能为空");
        }else if(verifyForm==4){
            return SysResult.fail("上级菜单只能为目录类型");
        }else if(verifyForm==5){
            return SysResult.fail("上级菜单只能为菜单类型");
        }

        sysAdminMenuService.updateById(menu);

        return SysResult.success();
    }

    /**
     * 删除
     */
    @SysLog("菜单删除")
    @ApiOperation(value = "菜单删除")
    @PostMapping("/delete/{menuId}")
    @RequiresPermissions("admin:menu:delete")
    public SysResult delete(@PathVariable("menuId") long menuId){
//        if(menuId <= 31){
//            return ResultVO.fail("系统菜单，不能删除");
//        }

        //判断是否有子菜单或按钮
        List<SysAdminMenus> menuList = sysAdminMenuService.queryListParentId(menuId);
        if(menuList.size() > 0){
            return SysResult.fail("请先删除子菜单或按钮");
        }

        sysAdminMenuService.delete(menuId);

        return SysResult.success();
    }
    /**
     * 验证参数是否正确
     */
    private Integer verifyForm(SysAdminMenus menu){
        if(StringUtils.isBlank(menu.getName())){
//            throw new RRException("菜单名称不能为空");
            return 1;
        }

        if(menu.getParentId() == null){
//            throw new RRException("上级菜单不能为空");
            return 2;
        }

        //菜单
        if(menu.getLevel() == 2){
            if(StringUtils.isBlank(menu.getPath())){
//                throw new RRException("菜单URL不能为空");
                return 3;
            }
        }

        //上级菜单类型
        int parentType = 1;
        if(menu.getParentId() != 0){
            SysAdminMenus parentMenu = sysAdminMenuService.getById(menu.getParentId());
            parentType = parentMenu.getLevel();
        }

        //目录、菜单
        if(menu.getLevel() == 1 ||
                menu.getLevel() == 2){
            if(parentType != 1){
//                throw new RRException("上级菜单只能为目录类型");
                return 4;
            }
            return 0;
        }

        //按钮
        if(menu.getLevel() == 3){
            if(parentType != 2){
//                throw new RRException("上级菜单只能为菜单类型");
                return 5;
            }
            return 0;
        }
        return 0;
    }
}
